const mongoose = require('mongoose');

let mongooseModel = mongoose.model('adminModel');
let merchantsModel = mongoose.model('merchantModel');
let shopsModel = mongoose.model('shopsModel');
//第三层
// 渲染管理员列表
module.exports.renderAdmin = async () => {
  let data = await mongooseModel.find();
  return data;
}

// 新增管理员
module.exports.addAdmin = async (addAdmin) => {
  let data = await mongooseModel.find({ name: addAdmin.name });
  if (data.length == 0) {
    return await mongooseModel.create(addAdmin);
  } else {
    return {}
  }
}

// 新增管理员
module.exports.deleteAdmin = async (params) => {
  return await mongooseModel.deleteOne(params);
}

//通过_id来获取一个管理员的数据
module.exports.oneAdmin = async (params) => {
  return await mongooseModel.find(params);
}

//修改管理员
module.exports.updateAdmin = async (params) => {
  return await mongooseModel.updateOne({ _id: params._id }, params);
}

//渲染商人列表
module.exports.getMerchantList = async () => {
  return await merchantsModel.find();
}

//冻结商人
module.exports.freezeMerchant = async (params) => {
  return await merchantsModel.updateOne({ _id: params._id }, { state: true });
}

//解冻商人
module.exports.unFreezeMerchant = async (params) => {
  return await merchantsModel.updateOne({ _id: params._id }, { state: false });
}

//门店列表
module.exports.getShopList = async () => {
  const data = await shopsModel.find();
  return data;
}

//门店通过审核
module.exports.handleSuccess = async (params) => {
  // return await shopsModel.updateOne({ _id: params._id }, { ...params, state: 2 });
  return await shopsModel.updateOne({ _id: params._id }, { state: 2 });
}

//门店审核失败
module.exports.handleFail = async (params) => {
  return await shopsModel.updateOne({ _id: params._id }, { state: 3 });
}
